/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cundong.recyclerview.sample.provider;

import com.cundong.recyclerview.ResourceTable;
import com.cundong.recyclerview.sample.bean.Contact;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * ContactItemProvider Class
 */
public class ContactItemProvider extends BaseItemProvider {
    private List<Contact> contactData;

    private Context context;

    /**
     * ContactItemProvider constructor
     *
     * @param context context
     * @param contactData contactData
     */
    public ContactItemProvider(Context context, List<Contact> contactData) {
        this.contactData = contactData;
        this.context = context;
    }

    @Override
    public int getCount() {
        return contactData.size();
    }

    @Override
    public Object getItem(int position) {
        return contactData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        Component container = getItemComponent(position);
        return container;
    }

    private Component getComponent(int position) {
        Contact contact = contactData.get(position);
        Component container = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_list_item, null, false);
        Component nameComponent = container.findComponentById(ResourceTable.Id_name);
        if (nameComponent instanceof Text) {
            Text name = (Text)nameComponent;
            name.setText(contact.getUserName());
        }
        Component telComponent = container.findComponentById(ResourceTable.Id_tel);
        if (telComponent instanceof Text) {
            Text tel = (Text)telComponent;
            tel.setText("tel:" + contact.getTelPhone());
        }
        return container;
    }

    private Component getItemComponent(int position) {
        Component container = getComponent(position);
        return container;
    }

    /**
     * addData
     *
     * @param list list
     */
    public void addData(ArrayList<Contact> list) {
        for (int index = 0; index < list.size(); index++) {
            contactData.add(list.get(index));
        }
        notifyDataChanged();
    }
}
